var imports = require("../function/imports.js");
var MyFs = require("../generic_class/MyFs");
var log = require("pomelo-logger").getLogger(__filename);

var autowire = function(packageName, beanName){
    if(arguments.length == 1){
        beanName = packageName;
        packageName = "root";
    }
    var packagePath = imports(packageName);

    var fileName = beanName + ".js";

    var haveFile = MyFs.isHaveFileInCurrentDir(packagePath, fileName);
    if(haveFile){
        return require(packagePath + "/" + fileName);
    }

    var filePaths = MyFs.getAllMatchFilePath(packagePath, fileName);
    if(filePaths.length == 1){
        return require(filePaths[0]);
    } else {
        log.error("文件夹路径" + packagePath +"文件不存在或者有相同的文件，请确认包名是否正确。");
        return null;
    }
}

module.exports = autowire;